package com.xyh.common.util;

import com.xyh.common.exception.BizRuntimeException;
import com.xyh.system.model.User;
import org.apache.shiro.SecurityUtils;

/**
 * @Author:kyle
 * @Date:2021/1/5 - 23:24
 * @Project:shirodemo
 * @Package:com.xyh.common.util
 * @Version:1.0
 */
public class ShiroUtil {

    /**
     * 获取从登陆信息中获取当前用户，不存在就抛出未登陆异常
     *
     * @return
     */
    public static User getCurrentUser() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            throw new BizRuntimeException(ResultCode.USER_NOT_LOGGED_IN);
        }
        return user;
    }
}
